package handlers;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.utils.Array;

public class MyContactListener implements ContactListener {

	int numberContact;
	private Array<Body> bodiesToRemove;
	private boolean playerDead;
	public MyContactListener() {
		super();
		bodiesToRemove = new Array<Body>();
	}

	@Override
	public void beginContact(Contact contact) {
		Fixture fa = contact.getFixtureA();
		Fixture fb = contact.getFixtureB();

		if (fa == null || fb == null)
			return;
		if (fa.getUserData() != null && fa.getUserData().equals("foot")) {
			numberContact++;
		}
		if (fb.getUserData() != null && fb.getUserData().equals("foot")) {
			numberContact++;
		}

		//
		if (fa.getUserData() != null && fa.getUserData().equals("crystal")) {
			// remove crystal
			bodiesToRemove.add(fa.getBody());
		}
		if (fb.getUserData() != null && fb.getUserData().equals("crystal")) {
			bodiesToRemove.add(fb.getBody());
		}
	}

	@Override
	public void endContact(Contact contact) {
		Fixture fa = contact.getFixtureA();
		Fixture fb = contact.getFixtureB();
		if (fa == null || fb == null)
			return;
		if (fa.getUserData() != null && fa.getUserData().equals("foot")) {
			numberContact--;
		}
		if (fb.getUserData() != null && fb.getUserData().equals("foot")) {
			numberContact--;
		}
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		// TODO Auto-generated method stub

	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		// TODO Auto-generated method stub

	}

	public boolean playerCanJump() {
		return numberContact > 0;
	}
	public boolean isPlayerDead() {
		return playerDead;
	}
	public Array<Body> getBodiesToRemove() {
		return bodiesToRemove;
	}
}
